package com.zaofan.dao;

import com.zaofan.dao.base.DaoSupportImpl;
import com.zaofan.mobile.bean.HotelInfo;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2016/12/20.
 */
public class HotelInfoDao extends DaoSupportImpl<HotelInfo> {


    public List<HotelInfo> findHotelByHotelNo(String no) {

        try {
            String sql = "select * from t_hotel_info where  hotelNo= '" +no+"'";
            System.err.println(sql);
            QueryRunner runner = new QueryRunner(getDataSource());
            return runner.query(sql, new BeanListHandler<>(HotelInfo.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }



    public HotelInfo findOpenHotelByHotelNo(String no) {

        try {
            String sql = "select * from t_hotel_info where status = 1 and hotelNo= '" +no+"'";
            System.err.println(sql);
            QueryRunner runner = new QueryRunner(getDataSource());
            return runner.query(sql, new BeanHandler<>(HotelInfo.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    public void incScanByHotelNo(String no) {
        try {
            String sql = "update t_hotel_info set scanCount = scanCount+1 where hotelNo= '" +no+"'";
            System.err.println(sql);
            QueryRunner runner = new QueryRunner(getDataSource());
            runner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void incOrderByHotelNo(String no){
        try {
            String sql = "update t_hotel_info set orderCount = orderCount+1 where  hotelNo= '" +no+"'";
            System.err.println(sql);
            QueryRunner runner = new QueryRunner(getDataSource());
            runner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteUserAdderss(String id) {
        try {
            String sql = "delete from address where id="+id;
            System.err.println(sql);
            QueryRunner runner = new QueryRunner(getDataSource());
            runner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteUserAdderssByOpenId(String openid) {
        try {
            String sql = "delete from address where mobile="+"'"+openid+"'";
            System.err.println(sql);
            QueryRunner runner = new QueryRunner(getDataSource());
            runner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void createHotel(String hotelno,int addressId){
        try {
            String sql = "insert into t_hotel_info (hotelNo,addressId,status) VALUES ('"+hotelno+"','"+addressId+"','"+1+"')";
            System.err.println(sql);
            QueryRunner runner = new QueryRunner(getDataSource());
            runner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
